package com.imu.purse.modules.sys.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.imu.core.base.BaseService;
import com.imu.enums.DStatus;
import com.imu.purse.modules.sys.dao.SysCarouselMapper;
import com.imu.purse.modules.sys.entity.SysCarouselEntity;
import com.imu.purse.modules.sys.service.SysCarouselService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 轮播图配置表
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2017-11-06 17:17:24
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class SysCarouselServiceImpl extends BaseService<SysCarouselEntity> implements SysCarouselService 
{
	@Override
    @Transactional
    public int save(final SysCarouselEntity sysCarousel)
	{
		sysCarousel.setStatus(DStatus.NOR.getType());
		sysCarousel.setTimeCreate(new Date());
    	sysCarousel.setTimeUpdate(new Date());
    	return super.insert(sysCarousel);
	}
	
	@Override
	@Transactional
    public int update(final SysCarouselEntity sysCarousel) 
    {
    	sysCarousel.setTimeUpdate(new Date());
    	return mapper.updateByPrimaryKeySelective(sysCarousel);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final SysCarouselEntity sysCarousel)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(sysCarousel)))
    	{
        	len = this.insert(sysCarousel);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	SysCarouselEntity dto = null;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
    			dto = new SysCarouselEntity();
    			dto.setCarId(Long.valueOf(id));
    			dto.setStatus(DStatus.DEL.getType());
        		this.update(dto);
    		}
		}
    	
    	return true;
    }
    
    @Override
    public SysCarouselEntity findById(final Long carId)
    {
    	if(null == carId) return null;
    	return mapper.selectByPrimaryKey(carId);
    }
    
    @Override
    public List<SysCarouselEntity> queryList(final SysCarouselEntity sysCarousel)
    {
    	if (sysCarousel.getPage() != null && sysCarousel.getLimit() != null)
    	{
            PageHelper.startPage(sysCarousel.getPage(), sysCarousel.getLimit());
        }
    	
    	return mapper.findByPage(sysCarousel);
    }
    
	@Autowired
	private SysCarouselMapper mapper;
}
